package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.ISSubstationDeviceService;
import com.spire.doc.*;
import com.spire.doc.collections.SectionCollection;
import com.spire.doc.collections.TableCollection;
import com.spire.doc.documents.Paragraph;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SProtectDeviceMapper;
import com.ruoyi.system.service.ISProtectDeviceService;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 保护装置信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-05
 */
@Service
public class SProtectDeviceServiceImpl extends ServiceImpl<SProtectDeviceMapper, SProtectDevice> implements ISProtectDeviceService
{

    @Autowired
    private SProtectDeviceMapper protectDeviceMapper;

    @Autowired
    private ISSubstationDeviceService substationDeviceService;

    @Override
    public String getProtectDeviceModelByDeviceId(String deviceId) {

        return protectDeviceMapper.getProtectDeviceModelByDeviceId(deviceId);
    }

    @Override
    public List getProtectDeviceList(String id) {
        List<SProtectDevice> list = protectDeviceMapper.getProtectDeviceList(id);
        return list;
    }

    @Override
    public String importData(MultipartFile file) throws Exception {

        ExcelUtil<SSubstationDevice> util = new ExcelUtil<SSubstationDevice>(SSubstationDevice.class);
        Sheet sheet = util.getSheet("", file.getInputStream());
        int lastRowNum = sheet.getLastRowNum();
        if(lastRowNum <= 0) return "数据为空！";

        // 创建变电站设备对象，用来保存表格中的前4行的值
        SSubstationDevice substationDevice = substationDeviceService.createSubstationDevice(sheet);

        String protectDeviceModel = substationDevice.getProtectDeviceModel();
        SProtectDevice orCreateByModel = getOrCreateByModel(protectDeviceModel);
        if(StringUtils.isNotNull(orCreateByModel)){
            return "导入成功！";
        }
        return "文件不存在保护型号！";
    }

    @Override
    public SProtectDevice getOrCreateByModel(String protectDeviceModel) {

        if(StringUtils.isBlank(protectDeviceModel)) return new SProtectDevice();

        SProtectDevice protectDevice = this.getOne(new LambdaQueryWrapper<SProtectDevice>()
                    .eq(SProtectDevice::getProtectDeviceModel, protectDeviceModel));
        if (StringUtils.isNull(protectDevice)) {
            protectDevice = new SProtectDevice();
            protectDevice.setCreateBy(SecurityUtils.getUserId().toString());
            protectDevice.setCreateTime(new Date());
            protectDevice.setProtectDeviceModel(protectDeviceModel);
            this.save(protectDevice);
        }
        return protectDevice;
    }

    @Override
    public boolean checkProtectDevice(SProtectDevice protectDevice) {
        if(StringUtils.isBlank(protectDevice.getProtectDeviceModel())){
            throw new ServiceException("保护装置型号不能为空！");
        }
        long count = this.count(new LambdaQueryWrapper<SProtectDevice>()
                .eq(SProtectDevice::getProtectDeviceModel, protectDevice.getProtectDeviceModel()));
        if(count > 0){
            throw new ServiceException("保护装置型号：" + protectDevice.getProtectDeviceModel() + " 已存在！");
        }
        return true;
    }
}
